package com.trw.trp.action;

import javax.mail.MessagingException;

import com.trw.trp.AppConfiguration;
import com.trw.trp.bean.UserBean;
import com.trw.trp.dao.DaoFactory;
import com.trw.trp.dao.UserDao;
import com.trw.trp.msg.ResultMessage;
import com.trw.trp.util.TRPConstants;
import com.trw.trp.util.TRPUtil;
import com.utuan.log.Log;
import com.utuan.log.LogFactory;

public class UserAction extends AbstractAction {
	
	private static Log log = LogFactory.getLog(UserAction.class);
	
	private String userId;
	private String password;
	private String userName;
	private String division;
	private String department;
	private String position;
	private String tel;
	private String mobile;
	private String userEmail;
	
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getDivision() {
		return division;
	}

	public void setDivision(String division) {
		this.division = division;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public void login() throws Exception {
		UserDao dao = DaoFactory.getUserDao();
		long userID = dao.login(userId, password);
		ResultMessage ret = new ResultMessage();
		if (userID > -1) {
			ret.setState(ResultMessage.SUCCESS);
			ret.setMessage(AppConfiguration.getInstance().getProperty("user.login.ok"));
			TRPUtil.removeFromSession("GOING_TO");
		} else {
			ret.setState(ResultMessage.FAILED);
			ret.setMessage(AppConfiguration.getInstance().getProperty("user.login.fail"));
		}
		writeJSON(ret);
		if (ret.getState() == ResultMessage.SUCCESS) {
			UserBean user = dao.getUser(userID);
			TRPUtil.putToSession(TRPConstants.CURRENT_USER, user);
		}
		return;
	}

	public ResultMessage verify(String userId) {
		UserDao dao = DaoFactory.getUserDao();
		return dao.verify(userId);
	}

	public String logout() {
		TRPUtil.removeFromSession(TRPConstants.CURRENT_USER);
		return SUCCESS;
	}

	public void register() throws Exception {
		ResultMessage ret = verify(userId);
		if (ret.getState() == ResultMessage.SUCCESS) {
			UserBean user = new UserBean();
			user.setEmail(userId);
			user.setName(userName);
			user.setDivision(division);
			user.setDepartment(department);
			user.setPosition(position);
			user.setTel(tel);
			user.setMobile(mobile);
			UserDao dao = DaoFactory.getUserDao();
			ret = dao.register(user, password);
		}
		writeJSON(ret);
	}

	public void resetPwd() throws Exception {
		UserDao dao = DaoFactory.getUserDao();
		String pwd = dao.getUserPwd(userEmail);
		ResultMessage ret = null;
		if (pwd != null) {
			String subject = AppConfiguration.getInstance().getProperty(
					"user.reset_pwd.mail.subject");
			String body = AppConfiguration.getInstance().getProperty("user.reset_pwd.mail.body")
					+ " " + pwd;
			try {
				TRPUtil.sendMail(userEmail, "", subject, body);
				ret = new ResultMessage(ResultMessage.SUCCESS, AppConfiguration.getInstance()
						.getProperty("email.sent.success"));
			} catch (MessagingException e) {
				log.error("Email sent failed!", e);
				ret = new ResultMessage(ResultMessage.FAILED, AppConfiguration.getInstance()
						.getProperty("email.sent.fail"));
			}
		} else {
			ret = new ResultMessage(ResultMessage.FAILED, AppConfiguration.getInstance()
					.getProperty("user.reset_pwd.fail"));
		}
		writeJSON(ret);
	}
}
